<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *售后列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['id']){
	$_SESSION['classid']=$_RQ['id'];
	$this->session('classid',$_RQ['id']);
}
if(!$_SESSION['classid']){
	exi('请选择售后群！','error');
}
$class=pdo_get('jy_yxw_class',array('id'=>$_SESSION['classid'],'uniacid'=>$_SESSION['uniacid']));
$tabs=array(
	array('op'=>'list','title'=>$class['title'].'-使用表'),
	array('op'=>'edit','title'=>'添加使用表'),
	array('op'=>'listadd','title'=>'导入使用表'),
);
//列表
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('storeid'=>$_SESSION['storeid'],'classid'=>$_SESSION['classid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$list=pdo_getslice('jy_yxw_class_kc',$params,array($pindex,$psize),$total,$fields,'','daytime desc,id desc');
	if($list){
		foreach($list as &$item){
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','del_message'=>'确认删除该使用表吗？','total'=>$total,'psize'=>$psize,'del_op'=>'del','html_to'=>'list_html','list'=>array(
			array('type'=>'string','title'=>'上课日期','name'=>'day'),
			array('name'=>'fuwuid','title'=>'所属使用','type'=>'select2','options'=>'fuwus'),
			array('name'=>'kejianid','title'=>'课件名称','type'=>'select2','options'=>'kejians'),
			array('name'=>'workerid','title'=>'售后技术姓名','type'=>'select2','options'=>'workers'),
			array('name'=>'status','title'=>'状态','type'=>'radio2','options'=>'status'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'课件名称','name'=>'title','help'=>''),
			array('name'=>'status','width'=>'15','title'=>'状态','type'=>'radio','options'=>'status'),
		),
	);
	$return['kejians']=$this->getall('kejian',array('fuwuid'=>$class['fuwuid'],'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['workers']=$this->getall('worker',array('juese'=>10,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['fuwus']=$this->getall('fuwu',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['status']=array('未上课','已上课','待补课');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yxw_class_kc',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回课件列表'),
		),
		'edit'=>array(
			array('type'=>'time','time_type'=>'day','title'=>'上课日期','name'=>'day'),
			array('name'=>'kejianid','title'=>'课件名称','type'=>'select','options'=>'kejians'),
			array('name'=>'workerid','title'=>'售后技术姓名','type'=>'select','options'=>'workers'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['kejians']=$this->getall('kejian',array('fuwuid'=>$item['fuwuid']?:$class['fuwuid'],'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['workers']=$this->getall('worker',array('juese'=>10,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['status']=array('未上课','已上课','待补课');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$this->model('fuwu')->add($postdata);
	exi('操作成功！','','goback');
}
if($_RQ['op']=='listadd'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$this->model('fuwu')->daoru($postdata);
		exi('导入成功','','/shouhou.class_kc/');
	}
	$return['item']=array('classtitle'=>$class['title'],'classid'=>$class['id'],'fuwuid'=>$class['fuwuid']);
	$return['item']['fuwutitle']=$this->getcolumn('fuwu',array('id'=>$class['fuwuid'],'uniacid'=>$_SESSION['uniacid']),'title');	
	$return['item']['excelurl']=SITEROOT.'/addons/jy_yxw/resource/addshouhou.csv';
	//$return['item']['excelurl']='请直接下载模板选择导入！';
	$htmls=array (
		  'type' => 'edit',
		  'tabs'=>$tabs,
		  'edit' => array (
				array ('type' => 'tip','title' => '<red>[温馨提示]：在导入过程中，请不要进行任何操作!</red></br>
				功能介绍：可将本模块导出的CSV文件快速上传,节约您的大量时间!</br>
				1、导入时会匹配【售后技术姓名】，如果不存在，将不保存售后技术姓名数据；</br>
				2、导入时会匹配【课件名称】，如果不存在，将自动添加一个课件到该使用旗下；</br>
				3、【上课日期】重复的会自动忽略；</br>
				4、注意<red>【售后技术姓名】，【课件名称】</red>的准确性！
				
				'),
				array('type'=>'copy','title'=>'售后群名称','name'=>'classtitle'),
				array('type'=>'copy','title'=>'使用名称','name'=>'fuwutitle'),
				array ('name' => 'excelurl','type' => 'copy','title' => 'CSV示例文件下载'),
				array ('name' => 'excel','type' => 'upfile','title' => '上传CSV','help'=>'<red>别的表复制内容到新表？如果两个表编码不一样，复制后，会导致新的表编码异常，会导致导入异常。解决办法：先把需被复制的表，统一改成UTF-8编码，然后再复制内容到新表，就行！</red>'),
		  ),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$class_kc=pdo_get('jy_yxw_class_kc',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			if($class_kc['status']){
				exi('只能删除未上课的','error');
			}
			pdo_delete('jy_yxw_class_kc',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}